﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    class LevelManager
    {
        public Texture2D m_TileSheet;
        public Tile[] m_TileProperties;
        public Vector2 m_TileSize;
        public Vector2 m_TileSheetSize;
        public Vector2 m_MapSize;

        private static LevelManager m_Instance = new LevelManager();

        public static LevelManager Instance
        {
            get
            {
                return m_Instance;
            }
        }

        public void Load(string szFile)
        {
            foreach (BaseObject b in ObjectManager.Instance.m_ObjectList)
            {
                if (b.m_ObjectType == BaseObject.OBJ_TYPE.OBJ_TILE)
                {
                    b.m_bIsAlive = false;
                }
                else if (b.m_ObjectType == BaseObject.OBJ_TYPE.OBJ_ENEMY)
                {
                    b.m_bIsAlive = false;
                }

            }

            FileStream tempFile = new FileStream("../../../Content/Maps/" + szFile, FileMode.Open);
            BinaryReader br = new BinaryReader(tempFile);
            br.ReadInt32(); // Read in and throw away version number
            m_TileSheet = GlobalContentManager.Instance.Texture("Tiles/"+Path.GetFileNameWithoutExtension(br.ReadString()));
            m_TileSheetSize = new Vector2(br.ReadInt32(), br.ReadInt32());
            m_TileSize = new Vector2(br.ReadInt32(), br.ReadInt32());
            m_TileProperties = new Tile[(int)m_TileSheetSize.X * (int)m_TileSheetSize.Y];
            for (int i = 0; i < m_TileProperties.Length; i++)
            {
                m_TileProperties[i] = new Tile();
                m_TileProperties[i].m_bIsSolid = br.ReadBoolean();
            }
            m_MapSize = new Vector2(br.ReadInt32(), br.ReadInt32());
            int NumTiles = br.ReadInt32();
            for (int i = 0; i < NumTiles; i++)
            {
                Tile tempTile = new Tile();
                tempTile.m_Position = new Vector2(br.ReadInt32()*m_TileSize.X, br.ReadInt32()*m_TileSize.Y);
                tempTile.m_fDepth = br.ReadInt32() * 100.0f;
                tempTile.m_nTileID = br.ReadInt32();
                ObjectManager.Instance.m_ObjectList.Add(tempTile);
            }
            int NumObjects = br.ReadInt32();
            for (int i = 0; i < NumObjects; i++)
            {
                string objType = br.ReadString();
                if (objType == "Portal")
                {
                    Portal tempPort = new Portal();
                    tempPort.m_Position = new Vector2(br.ReadInt32(), br.ReadInt32());
                    tempPort.m_DestPosition = new Vector2(br.ReadInt32(), br.ReadInt32());
                    tempPort.m_szDestFile = br.ReadString();
                    tempPort.m_nTrigger = br.ReadInt32();
                    ObjectManager.Instance.m_ObjectList.Add(tempPort);
                }
                else if (objType == "Enemy")
                {
                    Enemy tempEnemy = LoadEnemy("../../../Content/Enemies/" + br.ReadString());
                    tempEnemy.m_Position = new Vector2(br.ReadInt32(), br.ReadInt32());
                    ObjectManager.Instance.m_ObjectList.Add(tempEnemy);
                }
            }
            tempFile.Close();
        }

        private Enemy LoadEnemy(string szFile)
        {
            FileStream fsFile = new FileStream(szFile, FileMode.Open);
            BinaryReader br = new BinaryReader(fsFile);
            br.ReadString(); // Throw away enemy name;
            br.ReadString(); // Throw away texture name;
            string enemyClass = br.ReadString();
            fsFile.Close();
            switch (enemyClass)
            {
                case "Slime" :
                    {
                        Slime tempEnemy = new Slime(szFile);
                        return tempEnemy;
                    }
                case "Bat":
                    {
                        Bat tempEnemy = new Bat(szFile);
                        return tempEnemy;
                    }
                default:
                    {

                        break;
                    }
            }
            return null;
        }
    }
}
